package io.renren.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.google.common.base.Preconditions;
import io.renren.annotation.SysLog;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.entity.PeakEntity;
import io.renren.entity.UserPeakEntity;
import io.renren.service.PeakService;
import io.renren.service.UserPeakService;
import io.renren.util.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2019-01-20 12:55:41
 */
@RestController
@RequestMapping("api/userPeak")
public class UserPeakController {
    @Autowired
    private UserPeakService userPeakService;

    @Autowired
    private PeakService peakService;

    /**
     * 列表
     */
    @SysLog("查询UserPeak")
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = userPeakService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id) {
        UserPeakEntity update = userPeakService.selectById(id);

        return R.ok().put("data", update);
    }


    /**
     * 删除
     */
    @SysLog("删除UserPeak")
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] ids) {
        userPeakService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }


    /**
     * 根据项目查询评论
     */
    @RequestMapping("/infoByCID")
    public R infoByCID(@RequestParam Map<String, Object> params) {

        Preconditions.checkArgument(null != params.get("campaignId"), "活动ID不能为空");

        int campaignId = Integer.parseInt(params.get("campaignId").toString());

        Page<UserPeakEntity> peakEntityPage = userPeakService.selectPage(new Query<UserPeakEntity>(params).getPage(), new EntityWrapper<UserPeakEntity>().eq("campaign_id", campaignId));

        List<UserPeakEntity> records = peakEntityPage.getRecords();


        return R.ok().put("data", peakEntityPage);
    }


    /**
     * 列表
     */
    @RequestMapping("/listByUID")
    public R listByCid(@RequestParam Map<String, Object> params) {

        Preconditions.checkArgument(null != params.get("userId"), "用户ID不能为空");

        int uid = Integer.parseInt(params.get("userId").toString());

        Page<UserPeakEntity> peakEntityPage = userPeakService.selectPage(new Query<UserPeakEntity>(params).getPage(), new EntityWrapper<UserPeakEntity>().eq("user_id", uid));


        return R.ok().put("data", peakEntityPage);
    }


    /**
     * 保存
     */
    @SysLog("保存UserPeak")
    @Transactional(rollbackFor = Exception.class)
    @RequestMapping("/save")
    public R save(@RequestBody UserPeakEntity campaignPeak) {
        // update peak num
        PeakEntity peakEntity = peakService.selectById(campaignPeak.getPeakId());

        if(null != peakEntity) {
            peakEntity.setPeakNum(null == peakEntity.getPeakNum() ? 1 : peakEntity.getPeakNum() + 1);
            peakService.updateAllColumnById(peakEntity);
            userPeakService.insert(campaignPeak);
        }else{
           userPeakService.insert(campaignPeak);
        }


        return R.ok();
    }

}
